/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:NoticeAction.java
 * Version:1.0
 * Date:2012-1-2
 * Description: 
 * author:whh 
 */
package com.tietiao.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.common.bean.NoticeSetBean;
import com.tietiao.common.constants.SysUserConstants;
import com.tietiao.dao.model.SysMessage;
import com.tietiao.dao.model.SysUser;
import com.tietiao.service.bean.SysMessageBean;
import com.tietiao.service.midmessageuser.MidMessageUserService;
import com.tietiao.service.sysmessage.SysMessageService;

@Controller
@Scope("prototype")
public class SysMessageAction extends BaseAction{
	private static final long serialVersionUID = 6390116852580054814L;
	@Resource
	private  SysMessageService sysMessageService;
	@Resource
	private MidMessageUserService midMessageUserService;
	private Integer messageId;
	private Integer messageType=SysUserConstants.MESSAGE_TYPE_ATTENTION_ME;
	
	/**
	 * 功能：获得未读且设置提醒的消息
	 * @author: whh 
	 * @Date: 2012-1-2
	 * @return
	 */
	public String unRead(){
//		Object obj = request.getSession().getAttribute("remindList");
		List<SysMessage> resultList = new ArrayList<SysMessage>();
		SysUser sysUser = getUser();
		SysMessageBean messageBean = new SysMessageBean();
		messageBean.setUserId(sysUser.getUserId());
		List<SysMessage> list = sysMessageService.getUnReadGroupCount(messageBean);
		NoticeSetBean bean = sysUser.getNoticeSetBean();
		for (Iterator<SysMessage> iterator = list.iterator(); iterator.hasNext();) {
			SysMessage sysMessage =  iterator.next();
			if(sysMessage.getMessageType()==SysUserConstants.MESSAGE_TYPE_SYSTEM && bean.isSysNotice()){
				resultList.add(sysMessage);
			}else if(sysMessage.getMessageType()==SysUserConstants.MESSAGE_TYPE_RECOMMEND && bean.isReCommendNotice()){
				resultList.add(sysMessage);
			}else if(sysMessage.getMessageType()==SysUserConstants.MESSAGE_TYPE_COMMENT && bean.isCommentNotice()){
				resultList.add(sysMessage);
			}else if(sysMessage.getMessageType()==SysUserConstants.MESSAGE_TYPE_ATTENTION_ME && bean.isAttentionMeNotice()){
				resultList.add(sysMessage);
			}
		}
		request.getSession().setAttribute("remindList", resultList);
		return DEFAULT;
	}
	
	/**
	 * 功能：通知列表信息
	 * @author: whh 
	 * @Date: 2012-1-10
	 *
	 * @return
	 */
	public String list(){
		SysUser sysUser = getUser();
		SysMessageBean messageBean = new SysMessageBean();
		messageBean.setUserId(sysUser.getUserId());
		messageBean.setType(messageType);
		List<SysMessage> sysMessage = sysMessageService.getSysMessages(messageBean);
		sysMessageService.updateMessageRead(messageBean);
		request.setAttribute("sysMessage", sysMessage);
		return DEFAULT;
	}
	
	/**
	 * 
	 * 功能：删除消息记录
	 * @author: whh 
	 * @Date: 2012-1-2
	 * @throws IOException
	 */
	public void delete() throws IOException{
		response.setContentType("text/html");
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter writer =  response.getWriter();
		JSONObject object = new JSONObject();
		SysUser sysUser = getUser();
		int result = midMessageUserService.deleteMidMessageUser(messageId, sysUser.getUserId());
		object.put("result", result>0?true:false);
		writer.write(object.toString());
		writer.flush();
		writer.close();
	}

	public Integer getMessageId() {
		return messageId;
	}
	public void setMessageId(Integer messageId) {
		this.messageId = messageId;
	}
	public Integer getMessageType() {
		return messageType;
	}
	public void setMessageType(Integer messageType) {
		this.messageType = messageType;
	}
}
